// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.network.GameClient;
import zombie.network.packets.INetworkPacket;

public abstract class HitCharacterPacket implements INetworkPacket {
    private final HitCharacterPacket.HitType hitType;

    public HitCharacterPacket(HitCharacterPacket.HitType _hitType) {
        this.hitType = _hitType;
    }

    public static HitCharacterPacket process(ByteBuffer b) {
        byte _byte = b.get();
        if (_byte > HitCharacterPacket.HitType.Min.ordinal() && _byte < HitCharacterPacket.HitType.Max.ordinal()) {
            return (HitCharacterPacket)(switch (HitCharacterPacket.HitType.values()[_byte]) {
                case PlayerHitSquare -> new PlayerHitSquarePacket();
                case PlayerHitVehicle -> new PlayerHitVehiclePacket();
                case PlayerHitZombie -> new PlayerHitZombiePacket();
                case PlayerHitPlayer -> new PlayerHitPlayerPacket();
                case ZombieHitPlayer -> new ZombieHitPlayerPacket();
                case VehicleHitZombie -> new VehicleHitZombiePacket();
                case VehicleHitPlayer -> new VehicleHitPlayerPacket();
                default -> null;
            });
        } else {
            return null;
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putByte((byte)this.hitType.ordinal());
    }

    @Override
    public String getDescription() {
        return INetworkPacket.super.getDescription() + " (" + this.hitType.name() + ")";
    }

    public void tryProcess() {
        if (!GameClient.bClient
            || !HitCharacterPacket.HitType.VehicleHitZombie.equals(this.hitType) && !HitCharacterPacket.HitType.VehicleHitPlayer.equals(this.hitType)) {
            this.tryProcessInternal();
        } else {
            this.postpone();
        }
    }

    public void tryProcessInternal() {
        if (this.isConsistent()) {
            this.preProcess();
            this.process();
            this.postProcess();
            this.attack();
            this.react();
        } else {
            DebugLog.Multiplayer.warn("HitCharacter: check error");
        }
    }

    public abstract boolean isRelevant(UdpConnection connection);

    protected abstract void attack();

    protected abstract void react();

    protected void preProcess() {
    }

    protected void process() {
    }

    protected void postProcess() {
    }

    protected void postpone() {
    }

    public abstract boolean validate(UdpConnection connection);

    public static enum HitType {
        Min,
        PlayerHitSquare,
        PlayerHitVehicle,
        PlayerHitZombie,
        PlayerHitPlayer,
        ZombieHitPlayer,
        VehicleHitZombie,
        VehicleHitPlayer,
        Max;
    }
}
